package cn.unknowpupil.init.utils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created at 2021/1/4 by w10g <br>
 * Initialize MapSortUtils ... <br>
 *
 * @author unknowpupil
 * @since 0.0.1
 */
public class MapSortUtils {

    public static  <K extends Comparable<? super K>, V > Map<K, V> sortByKey(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap<>();

        map.entrySet().stream()
                .sorted(Map.Entry.<K, V>comparingByKey()
                        .reversed()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        return result;
    }

    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap<>();

        map.entrySet().stream()
                .sorted(Map.Entry.<K, V>comparingByValue()
                ).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        return result;
    }


}
